﻿using System;

using System.Collections.Generic;

using System.Linq;

using HalconDotNet;

namespace inspect
{
    public class InspectCtr
    {
        //string path = "Z:/";
        //string mntFile = "HD/1117N2YL.xml";

        enum PI { toleranceXY, toleranceR, padHueMa, padHueMi, padDeviationMa, padLRDeviationMa, BodyMa, BodyMi, BodyDeviationMa, BodyDeviationMi };
        enum RI { PX, PY, PR, Score, PHML, PHDL, PHMR, PHDR, PHDM, PHDD, BVM, BVD, posF, hueF, bodyF, passF }

        public DataS clDatas = new DataS();
        public void InspectComp()
        {
            HObject ho_Image;
            HTuple hv_out, hv_in;
            hv_in = new HTuple();



            MountData tMount = null;
            CompData tComp = null;


            HOperatorSet.GenEmptyObj(out ho_Image);
            ho_Image.Dispose();

            while (true)
            {
                string oneMonunt = "C11";
                HOperatorSet.ReadImage(out ho_Image, "C:/rgbset1/" + oneMonunt + ".png");

                clDatas.GetCompData(oneMonunt, ref tMount, ref tComp);
                hv_in = tComp.ParamS1;

                //角度设置
                InspectChip03.inspectChip(ho_Image, tComp.ModelID, hv_in, tMount.Pos.R, out hv_out);
                tMount.Result = hv_out;

                if (chipEvaluate(tComp.ParamS2, ref tMount.Result))
                    Console.WriteLine(oneMonunt + " is OK");
                else
                {
                    Console.Write(oneMonunt + " is NG");
                    Console.Write(" ,posF :" + tMount.Result[12]);
                    Console.Write(" ,hueF: " + tMount.Result[13]);
                    Console.WriteLine(" ,bodyF: " + tMount.Result[14]);

                }


            }



        }
        public bool chipEvaluate(int[] tParam, ref  int[] tResult)
        {
            //Pad Hue
            if (tResult[(int)RI.PHML] < tParam[(int)PI.padHueMa]
                && tResult[(int)RI.PHML] > tParam[(int)PI.padHueMi])
            {
                if (tResult[(int)RI.PHMR] < tParam[(int)PI.padHueMa]
                    && tResult[(int)RI.PHMR] > tParam[(int)PI.padHueMi])
                {
                    if (tResult[(int)RI.PHDL] < tParam[(int)PI.padDeviationMa]
                        && tResult[(int)RI.PHDR] < tParam[(int)PI.padDeviationMa])
                    {

                        if (tResult[(int)RI.PHDM] < tParam[(int)PI.padLRDeviationMa]
                            && tResult[(int)RI.PHDD] < tParam[(int)PI.padLRDeviationMa])
                        {
                            tResult[(int)RI.hueF] = 1;
                        }
                    }
                }
            }

            //Pos
            if (tResult[(int)RI.posF] == 1)
            {
                tResult[(int)RI.posF] = 0;
                if (tResult[(int)RI.PX] <= tParam[(int)PI.toleranceXY]
                    && tResult[(int)RI.PY] <= tParam[(int)PI.toleranceXY])
                {
                    if (tResult[(int)RI.PR] <= tParam[(int)PI.toleranceR])
                    {
                        if (tResult[(int)RI.Score] > 6)
                        {
                            tResult[(int)RI.posF] = 1;
                        }
                    }
                }
            }



            //if (tResult[(int)RI.PX] < tParam[(int)PI.toleranceX]
            //    && tResult[(int)RI.PY] < tParam[(int)PI.toleranceY]
            //    && tResult[(int)RI.PRD] < tParam[(int)PI.toleranceR])

            //Body
            if (tResult[(int)RI.BVM] < tParam[(int)PI.BodyMa]
                && tResult[(int)RI.BVM] > tParam[(int)PI.BodyMi]
                && tResult[(int)RI.BVD] < tParam[(int)PI.BodyDeviationMa]
                && tResult[(int)RI.BVD] > tParam[(int)PI.BodyDeviationMi]
                )
            {
                tResult[(int)RI.bodyF] = 1;
            }

            if (tResult[(int)RI.hueF] == 1
                && tResult[(int)RI.posF] == 1
                && tResult[(int)RI.bodyF] == 1)
            {
                tResult[(int)RI.passF] = 1;
                return true;
            }
            else
                return false;

        }
    }
}

